#!/bin/bash
GITDIR="/var/lib/jenkins/workspace"
PRONAME="yyg"
DSTDIR="/opt/"
DATE="`date +%F_%T`"
USER="root"
cat >> /tmp/ip.list <<EOF
10.2.11.237
10.2.11.233
EOF

if [ $# == 1 ];then
	VERSION=`echo $1`
        cd ${GITDIR}/${PRONAME}
	if [ $VERSION == 'latest' ];then
		git reset --hard HEAD^
	else
		git reset --hard $VERSION
	fi
	cd $GITDIR && tar zcf ${PRONAME}_${DATE}.tar.gz  ${PRONAME}/*
	mv ${PRONAME}_${DATE}.tar.gz /opt/${PRONAME}/
        for IP in `cat /tmp/ip.list`;do
		scp /opt/${PRONAME}/${PRONAME}_${DATE}.tar.gz ${USER}@${IP}:${DSTDIR}
		ssh ${USER}@${IP} "tar xf ${PRONAME}_${DATE}.tar.gz"
		ssh ${USER}@${IP} "ln -s /opt/${PRONAME}_${DATE} /var/www/html/${PRONAME}" 
		ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
        done
else
	cd ${GITDIR}/${PRONAME} && git pull
	cd $GITDIR && tar zcf ${PRONAME}_${DATE}.tar.gz  ${PRONAME}/*
	mv ${PRONAME}_${DATE}.tar.gz /opt/${PRONAME}/
        for IP in `cat /tmp/ip.list`;do
                scp /opt/${PRONAME}/${PRONAME}_${DATE}.tar.gz ${USER}@${IP}:${DSTDIR}
                ssh ${USER}@${IP} "tar xf ${PRONAME}_${DATE}.tar.gz"
                ssh ${USER}@${IP} "ln -s /opt/${PRONAME}_${DATE} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
        done
fi
	
